package com.infore.dataReceiving.controller;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.infore.dataReceiving.util.DateUtil;
import com.infore.dataReceiving.util.UUIDGenerator;

import io.swagger.annotations.ApiOperation;

@Controller
public class ImitateDataController {
    
    @Autowired
    DataSource DataSource;
    
    Connection connection = null;
    PreparedStatement pstmt = null;
    
    public static final List<String> indexCodeList = new ArrayList<String>(Arrays.asList("w01019","w21003","w21011","w21001"
            ,"w01001","w01010","w01009","w01014","w01003"));
    
    public static final List<String> checkCodeList = new ArrayList<String>(Arrays.asList("w01019","w21003","w21011","w21001"));
    
    @ApiOperation(value = "零点核查模拟数据接口")
    @RequestMapping(value = "/zeroCheck", method = RequestMethod.GET )
    @ResponseBody
    public void zeroCheckData() {
        try {
            connection = DataSource.getConnection();
            // 编写Sql
            String sql = "INSERT INTO ZEROCHECK_DATA (ZEROCHECK_ID,DATA_TIME,FACTOR_CODE,FLAG,MN,CHECK_DATA,STANDARD_VALUE,SPAN_VALUE) "
                    + "VALUES (?,TO_DATE(?,'yyyy-MM-dd HH24:mi:ss'),?,?,?,?,?,?) ";
            pstmt = connection.prepareStatement(sql);
            for (int i = 1; i <= 38; i++) {
                int x = 0;
                String stationCode = "F440600_FS" + i;
                for (int j = 1; j <= 20; j++) {
                    String time = "2019-11-08 6:00:00";
                    Date date = DateUtil.stringToDate(time, DateUtil.DATE_FULL_STR);
                    String hour = DateUtil.addDate(date, j, DateUtil.DATE_FULL_STR); 
                    for (String string : checkCodeList) {
                        x++;
                        String value = "";
                        String spanValue = "";
                        if ("w01019".equals(string)) { // 0-1
                            value = String.valueOf(new BigDecimal((Math.random() * 1))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            spanValue = "15";
                        } else if ("w21003".equals(string)) { // 0.01-0.03
                            value = String.valueOf(new BigDecimal(0.01 + (Math.random() * 0.03))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            spanValue = "2.5";
                        } else if ("w21011".equals(string)) { // 0.001-0.003
                            value = String.valueOf(new BigDecimal(0.001 + (Math.random() * 0.003))
                                    .setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue());
                            spanValue = "0.25";
                        } else if ("w21001".equals(string)) { // 0.01-0.1
                            value = String.valueOf(new BigDecimal(0.01 + (Math.random() * 0.1))
                                    .setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue());
                            spanValue = "5";
                        } else if ("w01001".equals(string)) {
                            value = String.valueOf(new BigDecimal(7 + (Math.random() * 1))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01010".equals(string)) {
                            value = String.valueOf(new BigDecimal(20 + (Math.random() * 5))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01009".equals(string)) {
                            value = String.valueOf(new BigDecimal(5 + (Math.random() * 2))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01014".equals(string)) {
                            value = String.valueOf(new BigDecimal(400 + (Math.random() * 20))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01003".equals(string)) {
                            value = String.valueOf(new BigDecimal(20 + (Math.random() * 15))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        }
                        String uuid = UUIDGenerator.getUUID();
                        // 判断是否存在
                        pstmt.setString(1, uuid);
                        pstmt.setString(2, hour);
                        pstmt.setString(3, string);
                        pstmt.setString(4, "N");
                        pstmt.setString(5, stationCode);
                        pstmt.setString(6, value);
                        pstmt.setString(7, "0");
                        pstmt.setString(8, spanValue);
                        pstmt.addBatch();
                        x++;
                        if (x % 20 == 0) {
                            pstmt.executeBatch();
                        }
                    }
                    pstmt.executeBatch();
                }
                System.out.println(stationCode + "站点插入" + x + "条数据！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
            } catch (SQLException e) {
                    e.printStackTrace();
            }
        }
    }
    
    @ApiOperation(value = "跨度核查模拟数据接口")
    @RequestMapping(value = "/spanCheck", method = RequestMethod.GET )
    @ResponseBody
    public void spanCheckData() {
        try {
            connection = DataSource.getConnection();
            // 编写Sql
            String sql = "INSERT INTO SPANCHECK_DATA (SPANCHECK_ID,DATA_TIME,FACTOR_CODE,FLAG,MN,CHECK_DATA,STANDARD_VALUE,SPAN_VALUE) "
                    + "VALUES (?,TO_DATE(?,'yyyy-MM-dd HH24:mi:ss'),?,?,?,?,?,?) ";
            pstmt = connection.prepareStatement(sql);
            for (int i = 1; i <= 38; i++) {
                int x = 0;
                String stationCode = "F440600_FS" + i;
                for (int j = 1; j <= 20; j++) {
                    String time = "2019-11-08 7:00:00";
                    Date date = DateUtil.stringToDate(time, DateUtil.DATE_FULL_STR);
                    String hour = DateUtil.addDate(date, j, DateUtil.DATE_FULL_STR); 
                    for (String string : checkCodeList) {
                        x++;
                        String value = "";
                        String standardValue = "";
                        String spanValue = "";
                        if ("w01019".equals(string)) { // 5-10
                            value = String.valueOf(new BigDecimal( 5 + (Math.random() * 10))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            spanValue = "15";
                            standardValue = "8";
                        } else if ("w21003".equals(string)) { // 0.7-0.9
                            value = String.valueOf(new BigDecimal(0.7 + (Math.random() * 0.9))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            spanValue = "2.5";
                            standardValue = "1";
                        } else if ("w21011".equals(string)) { // 0.001-0.003
                            value = String.valueOf(new BigDecimal(0.1 + (Math.random() * 0.3))
                                    .setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue());
                            spanValue = "0.25";
                            standardValue = "0.2";
                        } else if ("w21001".equals(string)) { // 0.01-0.1
                            value = String.valueOf(new BigDecimal(3 + (Math.random() * 5))
                                    .setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue());
                            spanValue = "5";
                            standardValue = "4";
                        } else if ("w01001".equals(string)) {
                            value = String.valueOf(new BigDecimal(7 + (Math.random() * 1))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01010".equals(string)) {
                            value = String.valueOf(new BigDecimal(20 + (Math.random() * 5))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01009".equals(string)) {
                            value = String.valueOf(new BigDecimal(5 + (Math.random() * 2))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01014".equals(string)) {
                            value = String.valueOf(new BigDecimal(400 + (Math.random() * 20))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01003".equals(string)) {
                            value = String.valueOf(new BigDecimal(20 + (Math.random() * 15))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        }
                        String uuid = UUIDGenerator.getUUID();
                        // 判断是否存在
                        pstmt.setString(1, uuid);
                        pstmt.setString(2, hour);
                        pstmt.setString(3, string);
                        pstmt.setString(4, "N");
                        pstmt.setString(5, stationCode);
                        pstmt.setString(6, value);
                        pstmt.setString(7, standardValue);
                        pstmt.setString(8, spanValue);
                        pstmt.addBatch();
                        x++;
                        if (x % 20 == 0) {
                            pstmt.executeBatch();
                        }
                    }
                    pstmt.executeBatch();
                }
                System.out.println(stationCode + "站点插入" + x + "条数据！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
            } catch (SQLException e) {
                    e.printStackTrace();
            }
        }
    }
    
    @ApiOperation(value = "加标回收率模拟数据接口")
    @RequestMapping(value = "/recoveryData", method = RequestMethod.GET )
    @ResponseBody
    public void recoveryData() {
        try {
            connection = DataSource.getConnection();
            // 编写Sql
            String sql = "INSERT INTO RECOVERY_DATA (RECOVERY_ID,DATA_TIME,FACTOR_CODE,FLAG,MN,WATER_TIME,WATER,CHECK_DATA,CHROMA_VALUE,VOLUME,DVOLUME) "
                    + "VALUES (?,TO_DATE(?,'yyyy-MM-dd HH24:mi:ss'),?,?,?,TO_DATE(?,'yyyy-MM-dd HH24:mi:ss'),?,?,?,?,?) ";
            pstmt = connection.prepareStatement(sql);
            for (int i = 1; i <= 38; i++) {
                int x = 0;
                String stationCode = "F440600_FS" + i;
                for (int j = 1; j <= 20; j++) {
                    String time = "2019-11-08 10:00:00";
                    Date date = DateUtil.stringToDate(time, DateUtil.DATE_FULL_STR);
                    String hour = DateUtil.addDate(date, j, DateUtil.DATE_FULL_STR); 
                    String waterTime = DateUtil.addHour(hour, 1, DateUtil.DATE_FULL_STR);
                    for (String string : checkCodeList) {
                        x++;
                        String value = "";
                        String water = "";
                        String chromaValue = "";
                        String volume = "";
                        String dvolume = "";
                        if ("w01019".equals(string)) { // 5-10
                            value = String.valueOf(new BigDecimal( 5 + (Math.random() * 15))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            water = String.valueOf(new BigDecimal( 2 + (Math.random() * 10))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            volume = String.valueOf(new BigDecimal((Math.random() * 1))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            chromaValue = "200";
                            dvolume = "40";
                        } else if ("w21003".equals(string)) { // 0.7-0.9
                            value = String.valueOf(new BigDecimal(1 + (Math.random() * 3))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            water = String.valueOf(new BigDecimal( 0.5 + (Math.random() * 2))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            volume = String.valueOf(new BigDecimal((Math.random() * 1))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            chromaValue = "400";
                            dvolume = "196";
                        } else if ("w21011".equals(string)) { // 0.001-0.003
                            value = String.valueOf(new BigDecimal(0.1 + (Math.random() * 0.5))
                                    .setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue());
                            water = String.valueOf(new BigDecimal( 0.1 + (Math.random() * 0.2))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            volume = String.valueOf(new BigDecimal( 1 + (Math.random() * 1.5))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            chromaValue = "50";
                            dvolume = "190";
                        } else if ("w21001".equals(string)) { // 0.01-0.1
                            value = String.valueOf(new BigDecimal(9 + (Math.random() * 9.5))
                                    .setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue());
                            water = String.valueOf(new BigDecimal( 6 + (Math.random() * 7))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            volume = String.valueOf(new BigDecimal(1 + (Math.random() * 1.5))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            chromaValue = "500";
                            dvolume = "190";
                        } else if ("w01001".equals(string)) {
                            value = String.valueOf(new BigDecimal(7 + (Math.random() * 1))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01010".equals(string)) {
                            value = String.valueOf(new BigDecimal(20 + (Math.random() * 5))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01009".equals(string)) {
                            value = String.valueOf(new BigDecimal(5 + (Math.random() * 2))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01014".equals(string)) {
                            value = String.valueOf(new BigDecimal(400 + (Math.random() * 20))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01003".equals(string)) {
                            value = String.valueOf(new BigDecimal(20 + (Math.random() * 15))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        }
                        String uuid = UUIDGenerator.getUUID();
                        // 判断是否存在
                        pstmt.setString(1, uuid);
                        pstmt.setString(2, waterTime);
                        pstmt.setString(3, string);
                        pstmt.setString(4, "N");
                        pstmt.setString(5, stationCode);
                        pstmt.setString(6, hour);
                        pstmt.setString(7, water);
                        pstmt.setString(8, value);
                        pstmt.setString(9, chromaValue);
                        pstmt.setString(10, volume);
                        pstmt.setString(11, dvolume);
                        pstmt.addBatch();
                        x++;
                        if (x % 20 == 0) {
                            pstmt.executeBatch();
                        }
                    }
                    pstmt.executeBatch();
                }
                System.out.println(stationCode + "站点插入" + x + "条数据！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
            } catch (SQLException e) {
                    e.printStackTrace();
            }
        }
    }
    
    @ApiOperation(value = "核查模拟数据接口")
    @RequestMapping(value = "/checkData", method = RequestMethod.GET )
    @ResponseBody
    public void checkData() {
        try {
            connection = DataSource.getConnection();
            // 编写Sql
            String sql = "INSERT INTO CHECK_DATA (CHECK_ID,DATA_TIME,FACTOR_CODE,FLAG,MN,CHECK_DATA,STANDARD_VALUE) "
                    + "VALUES (?,TO_DATE(?,'yyyy-MM-dd HH24:mi:ss'),?,?,?,?,?) ";
            pstmt = connection.prepareStatement(sql);
            for (int i = 1; i <= 38; i++) {
                int x = 0;
                String stationCode = "F440600_FS" + i;
                for (int j = 1; j <= 20; j++) {
                    String time = "2019-11-08 15:00:00";
                    Date date = DateUtil.stringToDate(time, DateUtil.DATE_FULL_STR);
                    String hour = DateUtil.addDate(date, j, DateUtil.DATE_FULL_STR); 
                    for (String string : checkCodeList) {
                        x++;
                        String value = "";
                        String standardValue = "";
                        if ("w01019".equals(string)) { // 5-10
                            value = String.valueOf(new BigDecimal( 5 + (Math.random() * 15))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            standardValue = "8";
                        } else if ("w21003".equals(string)) { // 0.7-0.9
                            value = String.valueOf(new BigDecimal(0.5 + (Math.random() * 1))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                            standardValue = "0";
                        } else if ("w21011".equals(string)) { // 0.001-0.003
                            value = String.valueOf(new BigDecimal(0.1 + (Math.random() * 0.3))
                                    .setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue());
                            standardValue = "0.2";
                        } else if ("w21001".equals(string)) { // 0.01-0.1
                            value = String.valueOf(new BigDecimal(1 + (Math.random() * 2))
                                    .setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue());
                            standardValue = "4";
                        } else if ("w01001".equals(string)) {
                            value = String.valueOf(new BigDecimal(7 + (Math.random() * 1))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01010".equals(string)) {
                            value = String.valueOf(new BigDecimal(20 + (Math.random() * 5))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01009".equals(string)) {
                            value = String.valueOf(new BigDecimal(5 + (Math.random() * 2))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01014".equals(string)) {
                            value = String.valueOf(new BigDecimal(400 + (Math.random() * 20))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        } else if ("w01003".equals(string)) {
                            value = String.valueOf(new BigDecimal(20 + (Math.random() * 15))
                                    .setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue());
                        }
                        String uuid = UUIDGenerator.getUUID();
                        // 判断是否存在
                        pstmt.setString(1, uuid);
                        pstmt.setString(2, hour);
                        pstmt.setString(3, string);
                        pstmt.setString(4, "N");
                        pstmt.setString(5, stationCode);
                        pstmt.setString(6, value);
                        pstmt.setString(7, standardValue);
                        pstmt.addBatch();
                        x++;
                        if (x % 20 == 0) {
                            pstmt.executeBatch();
                        }
                    }
                    pstmt.executeBatch();
                }
                System.out.println(stationCode + "站点插入" + x + "条数据！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
                if (pstmt != null) {
                    pstmt.close();
                }
            } catch (SQLException e) {
                    e.printStackTrace();
            }
        }
    }
}
